package in.muton.edushare.pages.user;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.PublicationsDAO;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.Publications;
import com.edushare.common.vo.Users;

import in.muton.edushare.helper.TinyMCEHelper;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.pages.publicpages.userpages.UserPublicBase;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;

public class EditPublicationsPanel extends EdusharePanel {

	private static final long serialVersionUID = -6805603885893702054L;
	private IModel<Users> userModel;
	private Boolean isEditMode = false;
	
	public EditPublicationsPanel(String id, IModel<Users> model, EduShareBasePage page) {
		super(id, model, page);
		this.userModel = model;
		add(CSSPackageResource.getHeaderContribution(UserPublicBase.class, "UserPublicBase.css"));
		final PublicationFormFragment pubForm = 
				new PublicationFormFragment("pubformdiv", this){
					@Override
					protected void onBeforeRender() {
						if(isEditMode)
							add(new SimpleAttributeModifier("style", "display:block;"));
						else
							add(new SimpleAttributeModifier("style", "display:none;"));
						super.onBeforeRender();
					}
				};
		pubForm.setOutputMarkupId(true).setMarkupId("pubformdiv");
		add(pubForm);		
		
		ListView<IModel<Publications>> pubs = 
			new ListView<IModel<Publications>>("jitem", new PropertyModel<List<IModel<Publications>>>(EditPublicationsPanel.this, "pubs")) 
			{

				private static final long serialVersionUID = -1455063265860283794L;

				@Override
				protected void populateItem(final ListItem<IModel<Publications>> item) {
					final Publications p = item.getModelObject().getObject();
					item.add(new Label("jtitle", p.getTitle()));
					item.add(new Label("jauthor", p.getAuthors()));
					item.add(new Label("jjournal", p.getJournal()));
					if(StringUtil.isValid(p.getLinkToPaper())){
						item.add(new ExternalLink("jjlink", p.getLinkToPaper()));
					}else{
						item.add(new EmptyPanel("jjlink"));
					}
					item.add(new Label("jabs", p.getAbstract()).setEscapeModelStrings(false));
					Form<Void> form = new Form<Void>("jform");
					item.add(form);
					form.add(new SubmitLink("jedit"){
						@Override
						public void onSubmit() {
							EditPublicationsPanel.this.isEditMode = true;
							pubForm.setPub(item.getModelObject());
						}
					});
					form.add(new SubmitLink("jdelete"){
						@Override
						public void onSubmit() {
							EditPublicationsPanel.this.isEditMode = false;
							pubForm.setPub(new CayenneModel<Publications>(new Publications()));
							new PublicationsDAO(getORMInterface()).delete(item.getModelObject().getObject());
						}
					});
				}
		};
		
		add(pubs);
	}
	
	public List<IModel<Publications>> getPubs(){
		ArrayList<IModel<Publications>> pl = new ArrayList<IModel<Publications>>();
		for(Publications p : userModel.getObject().getPublications())
			pl.add(new CayenneModel<Publications>(p));
		return pl;
	}
	
	public class PublicationFormFragment extends Fragment{

		private static final long serialVersionUID = 2351669689253079556L;
		private IModel<Publications> pub = new CayenneModel<Publications>(new Publications());
		
		public PublicationFormFragment(String id, MarkupContainer markupProvider) {
			super(id, "publicationFormFrag", markupProvider);
			Form<Void> f = new Form<Void>("pubForm");
			add(f);
			f.add(new TextField<String>("title", new PropertyModel<String>(this, "pub.object.title")));
			f.add(new TextField<String>("authors", new PropertyModel<String>(this, "pub.object.authors")));
			f.add(new TextField<String>("journal", new PropertyModel<String>(this, "pub.object.journal")));
			f.add(new TextField<String>("jlink", new PropertyModel<String>(this, "pub.object.linkToPaper")));
			TextArea<String> jabstract = new TextArea<String>("abstract", new PropertyModel<String>(this, "pub.object.abstract"));
			jabstract.add(TinyMCEHelper.getDefaultTinyMCEBehavior());
			f.add(jabstract);
			Button b = new Button("addPub"){

				private static final long serialVersionUID = -1844184752832792497L;

				@Override
				public void onSubmit() {
					EditPublicationsPanel.this.isEditMode = true;
					getPub().getObject().setUser(EditPublicationsPanel.this.userModel.getObject());
					
					//form validation
					if(!StringUtil.isValid(getPub().getObject().getTitle())){
						setError("Publication Title is required");
						return;
					}else if(getPub().getObject().getTitle()!=null && getPub().getObject().getTitle().length()>3000){
						setError("Publication Title is too long");
						return;
					}else if(getPub().getObject().getAuthors()!=null && getPub().getObject().getAuthors().length()>3000){
						setError("Authors field is too long");
						return;
					}else if(getPub().getObject().getJournal()!=null && getPub().getObject().getJournal().length()>3000){
						setError("Journal field is too long");
						return;
					}else if(getPub().getObject().getLinkToPaper()!=null && getPub().getObject().getLinkToPaper().length()>900){
						setError("Link to Full Paper is too long");
						return;
					}else if(getPub().getObject().getAbstract()!=null && getPub().getObject().getAbstract().length()>ApplicationConstants.MAX_DESC_SIZE){
						setError("Abstract is too long");
						return;
					}
					
					new PublicationsDAO(EditPublicationsPanel.this.getORMInterface())
						.addNewPublication(getPub().getObject());
					//clear on success
					clearError();
					EditPublicationsPanel.this.isEditMode = false;
					PublicationFormFragment.this.setPub(new CayenneModel<Publications>(new Publications()));
				}
			};
			f.add(b);
		}
		
		public IModel<Publications> getPub(){
			if(pub==null || pub.getObject()==null){
				pub = new CayenneModel<Publications>(new Publications());
			}
			return this.pub;
		}

		public void setPub(IModel<Publications> pub) {
			this.pub = pub;
		}
	}

}
